package com.question.mapper;

import com.question.pojo.entity.Major;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MajorMapper {

    /**
     * 获取所有专业
     *
     * @return 专业列表
     */
    @Select("SELECT major_id, major_name, description FROM majors")
    List<Major> selectAllMajors();

    // 新增
    @Insert("INSERT INTO majors (major_name, description) VALUES (#{majorName}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "majorId")
    void insertMajor(Major major);

    // 删除
    @Delete("DELETE FROM majors WHERE major_id = #{majorId}")
    int deleteMajor(Integer majorId);

    // 更新
    @Update("UPDATE majors SET major_name = #{majorName}, description = #{description} WHERE major_id = #{majorId}")
    int updateMajor(Major major);

    // 根据ID查询
    @Select("SELECT major_id, major_name, description FROM majors WHERE major_id = #{majorId}")
    Major getMajorById(Integer majorId);

    /**
     * 根据用户ID获取专业ID
     * @param userId
     * @return
     */
    @Select("select m.major_id, m.major_name, m.description from user u inner join majors m " +
            "on m.major_name = TRIM(u.professional) " +
            "where u.user_id = #{userId}")
    Major getMajorByUserId(Long userId);
}